package cn.edu.xcu.ckl.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import cn.edu.xcu.ckl.R;
import cn.edu.xcu.ckl.entity.CarInfo;
import cn.edu.xcu.ckl.utils.GlideEngine;

public class CarListAdapter extends RecyclerView.Adapter<CarListAdapter.MyHolder> {

    private List<CarInfo> mCarInfoList = new ArrayList<>();

    private Context mContext;

    public CarListAdapter(Context context) {
        mContext = context;
    }

    public void setCarInfoList(List<CarInfo> list) {
        if (list == null) {
            mCarInfoList.clear();
        } else {
            this.mCarInfoList = list;
        }
        //这句话不能少
        notifyDataSetChanged();
    }

    public List<CarInfo> getCarInfoList() {
        return mCarInfoList;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.car_list_item, null);

        return new MyHolder(view);
    }

    @Override
    @SuppressLint("RecyclerView")
    public void onBindViewHolder(@NonNull MyHolder holder,  int position) {

        //绑定数据
        CarInfo carInfo = mCarInfoList.get(position);
        holder.product_title.setText(carInfo.getProduct_title());
        holder.product_price.setText(carInfo.getProduct_price() + "");
        holder.product_count.setText(carInfo.getProduct_count() + "");
        GlideEngine.createGlideEngine().loadImage(mContext, carInfo.getProduct_img(),holder.product_img);

        //设置点击事件
        holder.btn_subtract.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mOnItemClickListener) {
                    mOnItemClickListener.onSubTractOnClick(carInfo, position);
                }
            }
        });

        holder.btn_plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mOnItemClickListener) {
                    mOnItemClickListener.onPlusOnClick(carInfo, position);
                }
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.delOnClick(carInfo, position);
                }

                return true;
            }
        });


    }

    @Override
    public int getItemCount() {
        return mCarInfoList.size();
    }

    static class MyHolder extends RecyclerView.ViewHolder {
        ImageView product_img;
        TextView product_title;
        TextView product_price;
        TextView product_count;

        TextView btn_subtract;
        TextView btn_plus;

        public MyHolder(@NonNull View itemView) {
            super(itemView);

            product_img = itemView.findViewById(R.id.product_img);
            product_title = itemView.findViewById(R.id.product_title);
            product_price = itemView.findViewById(R.id.product_price);
            product_count = itemView.findViewById(R.id.product_count);
            btn_subtract = itemView.findViewById(R.id.btn_subtract);
            btn_plus = itemView.findViewById(R.id.btn_plus);

        }
    }


    private onItemClickListener mOnItemClickListener;


    public void setOnItemClickListener(onItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public interface onItemClickListener {
        void onPlusOnClick(CarInfo carInfo, int position);

        void onSubTractOnClick(CarInfo carInfo, int position);

        void delOnClick(CarInfo carInfo, int position);
    }

}
